import java.util.*;

public class CompositePattern {
    public static void main(String[] args) {
        Component root = new Composite("root");
        root.add(new Leaf("leaf1"));
      Component component1=  new Composite("composite1");
      component1.add(new Leaf("leaf2"));
        root.add(component1);
        root.display(0);
    }
}

abstract class Component {
    void add(Component component) {
        System.out.println("不支持");
    };

    void remove(Component component) {
        System.out.println("不支持");
    };

    abstract void display(int depth);
}

class Composite extends Component {
    List<Component> list = new ArrayList<>();
    String name;

    public Composite(String name) {
        this.name = name;
    }

    @Override
    void display(int depth) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < depth; i++) {
                sb.append("-");
        }
        System.out.println(sb.toString()+name);
        for (Component component : list) {
            component.display(depth+2);
        }
    }

    @Override
    void add(Component component) {
        list.add(component);
    }

    @Override
    void remove(Component component) {
        list.remove(component);
    }

    public List<Component> getList() {
        return list;
    }

    public void setList(List<Component> list) {
        this.list = list;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class Leaf extends Component {

    String name;

    public Leaf(String name) {
        this.name = name;
    }

    @Override
    void display(int depth) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < depth; i++) {
                sb.append("-");
        }
        System.out.println(sb.toString()+name);
    }

}
